package com.quectel.business.common.parking.charging;

import cn.hutool.core.util.ClassUtil;
import com.quectel.constant.core.parking.ParkingChargingConstants;
import com.quectel.core.module.parking.dto.ParkingChargingDto;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 在这里编写说明
 *
 * @author: ryan
 * @email: ryan.ran@quectel.com
 * @date: 2023-05-10 14:29:00
 */
@Slf4j
public abstract class BaseOrderChargingCalc {

    private static final  Map<Byte, BaseOrderChargingCalc> CALCULATOR = new HashMap<>();

    static {
        Set<Class<?>> classes = ClassUtil.scanPackageBySuper(BaseOrderChargingCalc.class.getPackage().getName(), BaseOrderChargingCalc.class);
        for (Class<?> aClass : classes) {
            try {
                BaseOrderChargingCalc o = (BaseOrderChargingCalc)aClass.newInstance();
                CALCULATOR.putIfAbsent(o.calcType(), o);
            } catch (InstantiationException | IllegalAccessException e) {
                log.warn("Parking charging component init exception.");
            }
        }
    }

    /**
     *
     * @param type 计算方式
     * @return
     * @see ParkingChargingConstants.ChargingTypeEnum
     */
    public static BaseOrderChargingCalc getCalcByType(Byte type) {
        return CALCULATOR.get(type);
    }

    public abstract Byte calcType();

    /**
     * 停车费用计算
     * @param inTime 进时间
     * @param outTime 出时间
     * @param chargingConf 收费配置
     * @return
     */
    public abstract BigDecimal calc(Date inTime, Date outTime, ParkingChargingDto chargingConf);
}
